

let cfg = require('./utils/cfg');

class Task{

    /**
     *
     * @param target
     * @param func
     * @param delay
     */
    constructor(target,func,delay,once = false) {
        this._id = cfg.genId();
        this._target = target;
        this._func = func;
        this._bOnce = once;
        this._bValid = true;

        this._delay = delay;
        this._elapsed = 0;
        this._bStart = false;
    }

    get valid(){
        return this._bValid;
    }
    set valid(value){
        this._bValid = value;
    }

    get target(){
        return this._target;
    }

    get id(){
        return this._id;
    }

    update(dt){

        if(!this._bStart){
            this._elapsed += dt;
            if(this._elapsed >= this._delay){
                this._bStart = true;
            }else{
                return;
            }
        }

        if(this._target.visible && this._target.enabled){
            this._func.call(this._target,dt);
            if(this._bOnce){
                cfg.gameApp.scheduler.removeTask(this);
            }
        }
    }

    equals(task){
        if(
            this._target.id === task._target.id &&
            this._func === task._func
        ){
            return true;
        }
        return  false;
    }
}

module.exports = Task;